<script setup>
import { ref ,watch} from "vue";
import { useUserInfoStore } from "../../stores/userInfo.js";
const userInfoStore = useUserInfoStore();


const userInfo = ref({
  ...userInfoStore.info,
});

const rules = {

  email: [
    { required: true, message: "请输入用户邮箱", trigger: "blur" },
    { type: "email", message: "邮箱格式不正确", trigger: "blur" },
  ],

  phoneNumber: [
    { required: true, message: "请输入手机号码", trigger: "blur" },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: "手机号码格式不正确",
      trigger: "blur",
    },
  ],
};

watch(userInfoStore.info, (newValue) => {
    if (newValue) {

      userInfo.value.accountBalance = newValue.accountBalance;
    }
});


import { updateUserInfoService, rechargeService } from "../../api/user.js";

import { ElMessage } from "element-plus";

const updateUserInfoModel = ref({

  phoneNumber: '',
  email: '',

});

const updateUserInfo = async () => {

  updateUserInfoModel.value.phoneNumber = userInfo.value.phoneNumber;
  updateUserInfoModel.value.email = userInfo.value.email;


  let result = await updateUserInfoService(updateUserInfoModel.value);

  if (result.code === 200) {

    ElMessage.success("修改成功");

    userInfoStore.setInfo(userInfo.value);

  } else {
    ElMessage.error(result.msg);
  }
};

const rechargeAmount = ref(0.00);

const recharge = async () => {

  let params = new URLSearchParams();
  params.append("amount", rechargeAmount.value);

  let result = await rechargeService(params);

  if (result.code === 200) {
    ElMessage.success("充值成功");
    userInfoStore.info.accountBalance += rechargeAmount.value;
  } else {
    ElMessage.error(result.msg);
  }

}




</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>修改密码</span>
      </div>
    </template>
    <el-row>
      <el-col :span="12">
        <el-form :model="userInfo" :rules="rules" label-width="100px" size="large">
          <el-form-item label="登录名称">
            <el-input v-model="userInfo.userName" disabled></el-input>
          </el-form-item>


          <el-form-item label="账户余额">
            <el-row :gutter="20">
              <el-col :span="8">
                <el-input v-model="userInfo.accountBalance" disabled></el-input>
              </el-col>
              <el-col :span="8">
                <el-input type="number" v-model="rechargeAmount" placeholder="请输入金额"></el-input>
              </el-col>
              <el-col :span="6">
                <el-button type="primary" @click="recharge()">充值</el-button>
              </el-col>
            </el-row>
          </el-form-item>





          <el-form-item label="手机号码" prop="nickname">
            <el-input v-model="userInfo.phoneNumber"></el-input>
          </el-form-item>
          <el-form-item label="用户邮箱" prop="email">
            <el-input v-model="userInfo.email"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="updateUserInfo()">提交修改</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </el-card>
</template>
